//
//  HNSH_SafeItemView.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/22.
//

#import "HNSH_SafeItemView.h"

@interface HNSH_SafeItemView ()
/* titleLabel */
@property (nonatomic, strong) UILabel *titleLabel;
/* lineView */
@property (nonatomic, strong) UIView *lineView;
/* sendButton */
@property (nonatomic, strong) UIButton *sendButton;
@end

@implementation HNSH_SafeItemView

- (instancetype)initWithFrame:(CGRect)frame type:(NSInteger)type {
    self = [super initWithFrame:frame];
    if (self) {
        _type = type;
        [self configUI];
    }
    return self;
}

- (void)setTitleString:(NSString *)titleString {
    _titleString = titleString;
    self.titleLabel.text = titleString;
}

- (void)setContentString:(NSString *)contentString {
    _contentString = contentString;
    self.textField.text = contentString;
}

- (void)setPlaceholder:(NSString *)placeholder {
    _placeholder = placeholder;
    self.textField.placeholder = placeholder;
    NSMutableAttributedString *placeholderString = [[NSMutableAttributedString alloc] initWithString:self.textField.placeholder];
    [placeholderString addAttribute:NSForegroundColorAttributeName
                            value:[UIColor appGrayLabelColor]
                            range:NSMakeRange(0, self.textField.placeholder.length)];
    [placeholderString addAttribute:NSFontAttributeName
                            value:[UIFont systemFontOfSize:14]
                            range:NSMakeRange(0, self.textField.placeholder.length)];
    self.textField.attributedPlaceholder = placeholderString;
}

- (void)configUI {
    self.backgroundColor = [UIColor appWhiteLabelColor];
    self.layer.cornerRadius = 10.0f;
    self.layer.masksToBounds = YES;
    
    [self addSubview:self.titleLabel];
    [self addSubview:self.lineView];
    [self addSubview:self.textField];
    [self addSubview:self.sendButton];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(28);
        make.top.bottom.mas_equalTo(0);
    }];
    
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_right).offset(6);
        make.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(200);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(1);
    }];
    
    [self.sendButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.centerY.mas_equalTo(0);
        make.width.mas_equalTo(98);
        make.height.mas_equalTo(28);
    }];
    
    self.sendButton.hidden = self.type != 1;
}

#pragma mark lazy
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.text = @"";
        _titleLabel.font = [UIFont boldSystemFontOfSize:14];
        _titleLabel.textColor = [UIColor appBlack333LabelColor];
    }
    return _titleLabel;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc]init];
        _lineView.backgroundColor = [UIColor colorWithHexString:@"#EDEDED"];
    }
    return _lineView;
}

- (UITextField *)textField {
    if (!_textField) {
        _textField = [[UITextField alloc]init];
        _textField.textColor = [UIColor appBlackLabelColor];
        _textField.font = [UIFont systemFontOfSize:14];
        WeakSelf(weakSelf);
        [[_textField rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(__kindof UIControl * _Nullable x) {
            UITextField *field = x;
            if (weakSelf.fieldBlock) {
                weakSelf.fieldBlock(field.text);
            }
        }];
    }
    return _textField;
}

- (UIButton *)sendButton {
    if (!_sendButton) {
        _sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sendButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_sendButton setTitleColor:[UIColor appGreenColor] forState:UIControlStateNormal];
        _sendButton.titleLabel.font = [UIFont systemFontOfSize:14];
//        _sendButton.layer.cornerRadius = 10;
//        _sendButton.layer.masksToBounds = YES;
//        _sendButton.layer.borderColor = [[UIColor colorWithHexString:@"#47C4C2"] CGColor];
//        _sendButton.layer.borderWidth = 1.0f;
        _sendButton.hidden = YES;
        WeakSelf(weakSelf);
        [[_sendButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            UIButton *button = x;
            if (weakSelf.buttonBlock) {
                weakSelf.buttonBlock(button);
            }
        }];
    }
    return _sendButton;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
